<template>
  <view class="params-page">
    <view v-for="item in paramsList" :key="item.value">
      <view class="params-contain">
        <view class="params-name">
          {{ item.name }}
        </view>
        <view class="params-detail" @click="handleCheck(item)">
          <view class="">
            {{ item.txt }}
          </view>
          <view class="" style="color: #606266; font-size: 28rpx">
            <text v-if="item.type == 'temp'">℃</text>
            <text v-if="item.type == 'hour'">小时</text>
            <text v-if="item.type == 'min'">分钟</text>
            <text v-if="item.type == 'W'">W</text>
            <text v-if="item.type == 'V'">V</text>
            <text v-if="item.type == '%'">%</text>
            <up-icon name="arrow-down" v-if="item.type == 'icon'"></up-icon>
          </view>
        </view>
        <view class="params-btn">
          <up-button type="primary" size="default" @click="confirm(item)"
            >确认</up-button
          >
        </view>
      </view>
    </view>
    <up-picker
      :columns="actionList"
      :title="actionTitle"
      :show="actionSheet"
      @confirm="(e) => handleTempConfirm(e)"
      @cancel="handleClose"
      @close="handleClose"
      :defaultIndex="selected"
    ></up-picker>
  </view>
</template>

<script setup>
import { ref } from "vue";
import { onLoad, onShow } from "@dcloudio/uni-app";

const actionSheet = ref(false);
const actionTitle = ref("");
const actionConfirm = ref("");
const actionList = ref([]);
const actionDetail = ref({});
const identity = ref(null);
const selected = ref([]);
const paramsList = ref([
  {
    name: "温度校正",
    type: "temp",
    value: 1,
  },
  {
    name: "温度上限",
    type: "temp",
    value: 2,
  },
  {
    name: "温度下限",
    type: "temp",
    value: 3,
  },
  {
    name: "温控容差",
    type: "temp",
    value: 4,
  },
  {
    name: "过热保护温度",
    type: "temp",
    value: 5,
  },
  {
    name: "过热保护容差",
    type: "temp",
    value: 6,
  },
  {
    name: "防冻温度",
    type: "icon",
    value: 7,
  },
  {
    name: "加热器保护触发时间",
    type: "hour",
    value: 8,
  },
  {
    name: "加热器保护时间",
    type: "min",
    value: 9,
  },
  {
    name: "湿度校准",
    type: "%",
    value: 14,
  },
  {
    name: "电压保护阈值",
    type: "V",
    value: 10,
  },
  {
    name: "功率保护阈值",
    type: "W",
    value: 11,
  },
  {
    name: "电压校准",
    type: "V",
    value: 12,
  },
  {
    name: "功率校准",
    type: "W",
    value: 13,
  },
  {
    name: "继电器反向设置",
    type: "icon",
    value: 15,
  },
  {
    name: "断电记忆",
    type: "icon",
    value: 16,
  },
]);
const details = ref({});
onLoad((options) => {
  let code = decodeURIComponent(options.id);
  if (code) {
    getDevInfo(code);
  }
  identity.value = uni.getStorageSync("identity");
});

const confirm = (item) => {
  if (identity.value == false) {
    // 0  解锁  1 锁后台  2 开放开关机 3 全锁
    if (
      details.value.devKeyLock == 1 ||
      details.value.devKeyLock == 2 ||
      details.value.devKeyLock == 3
    ) {
      uni.showToast({
        title: "设备已锁定,无法操作",
        icon: "none",
      });
      return;
    }
  }
  let data = {
    id: details.value.id,
    equipNo: details.value.equipNo,
  };
  if (item.name == "温度校正") {
    data.tempAdj = item.txt;
  }
  if (item.name == "温度上限") {
    data.tempSetTop = item.txt;
  }
  if (item.name == "温度下限") {
    data.tempSetBot = item.txt;
  }
  if (item.name == "温控容差") {
    data.tolr = item.txt;
  }
  if (item.name == "过热保护温度") {
    data.overheat = item.txt;
  }
  if (item.name == "过热保护容差") {
    data.otolr = item.txt;
  }
  if (item.name == "防冻温度") {
    if (item.txt == "关闭防冻") {
      data.antifreeze = 0;
    } else {
      data.antifreeze = item.txt;
    }
  }
  if (item.name == "加热器保护触发时间") {
    data.triggerTm = item.txt;
  }
  if (item.name == "加热器保护时间") {
    data.protectTm = item.txt;
  }
  if (item.name == "湿度校准") {
    data.humAdj = item.txt;
  }
  if (item.name == "电压保护阈值") {
    data.overVoltage = item.txt;
    if (item.txt == "关闭") {
      data.overVoltage = 0;
    } else {
      data.overVoltage = item.txt;
    }
  }
  if (item.name == "功率保护阈值") {
    data.overPower = item.txt;
    if (item.txt == "关闭") {
      data.overPower = 0;
    } else {
      data.overPower = item.txt;
    }
  }
  if (item.name == "电压校准") {
    data.volAdj = item.txt;
  }
  if (item.name == "功率校准") {
    data.powerAdj = item.txt;
  }
  if (item.name == "继电器反向设置") {
    if (item.txt == "反向") {
      data.invert = 1;
    } else {
      data.invert = 0;
    }
  }
  if (item.name == "断电记忆") {
    if (item.txt == "上电开机") {
      data.powerRem = 2;
    } else if (item.txt == "上电关机") {
      data.powerRem = 1;
    } else {
      data.powerRem = 0;
    }
  }

  uni.$u.http.post("/wechat/editSend", data).then((res) => {
    if (res.success) {
      uni.showToast({
        title: "保存成功",
        icon: "success",
      });
    }
  });
};
const handleCheck = (item) => {
  if (identity.value == false) {
    // 0  解锁  1 锁后台  2 开放开关机 3 全锁
    if (
      details.value.devKeyLock == 1 ||
      details.value.devKeyLock == 2 ||
      details.value.devKeyLock == 3
    ) {
      uni.showToast({
        title: "设备已锁定,无法操作",
        icon: "none",
      });
      return;
    }
  }
  actionTitle.value = item.name;
  actionSheet.value = true;
  actionDetail.value = item;
  if (item.name == "温度校正") {
    // -9~9,默认0
    actionList.value.push(generateStepArray(-9, 9, 1));
  }
  if (item.name == "温度上限") {
    // 5-85，默认50
    actionList.value.push(generateStepArray(5, 85, 1));
  }
  if (item.name == "温度下限") {
    // 2-50，默认5
    actionList.value.push(generateStepArray(2, 50, 1));
  }
  if (item.name == "温控容差") {
    // 1-15，默认2
    actionList.value.push(generateStepArray(1, 15, 1));
  }
  if (item.name == "过热保护温度") {
    // 15-85，默认60
    actionList.value.push(generateStepArray(15, 85, 1));
  }
  if (item.name == "过热保护容差") {
    // 1-25，默认5
    actionList.value.push(generateStepArray(1, 25, 1));
  }
  if (item.name == "防冻温度") {
    // 2-15，默认关闭防冻
    actionList.value.push(generateStepArray(2, 15, 1));
    actionList.value[0].unshift("关闭防冻");
  }
  if (item.name == "加热器保护触发时间") {
    // 0-99，默认0
    actionList.value.push(generateStepArray(0, 99, 1));
  }
  if (item.name == "加热器保护时间") {
    // 10-90，默认60，步数10
    actionList.value.push(generateStepArray(10, 90, 10));
  }
  if (item.name == "湿度校准") {
    // -30-30，默认0，步数1
    actionList.value.push(generateStepArray(-30, 30, 1));
  }
  if (item.name == "电压保护阈值") {
    actionList.value.push(generateStepArray(90, 300, 10));
    actionList.value[0].unshift("关闭");
  }
  if (item.name == "功率保护阈值") {
    actionList.value.push(generateStepArray(4000, 6000, 100));
    actionList.value[0].unshift("关闭");
  }
  if (item.name == "电压校准") {
    actionList.value.push(generateStepArray(-100, 100, 1));
  }
  if (item.name == "功率校准") {
    actionList.value.push(generateStepArray(-1000, 1000, 10));
  }
  if (item.name == "继电器反向设置") {
    let arr = ["同向", "反向"];
    actionList.value = [arr];
  }
  if (item.name == "断电记忆") {
    let arr = ["断电记忆", "上电关机", "上电开机"];
    actionList.value = [arr];
  }
  defaultCHeck(actionList.value[0]);
};
// 默认选中
const defaultCHeck = (arr) => {
  arr.forEach((it, index) => {
    if (arr[index] == actionDetail.value.txt) {
      selected.value = [index];
    }
  });
};
const generateStepArray = (start, end, step) => {
  const result = [];
  for (let i = start; i <= end; i += step) {
    result.push(i);
  }
  return result;
};
const handleClose = () => {
  actionSheet.value = false;
  actionList.value = [];
  actionDetail.value = {};
  actionTitle.value = "";
  selected.value = [];
};
const handleTempConfirm = (e, item) => {
  actionDetail.value.txt = e.value[0];
  handleClose();
};

const getDevInfo = async (devId) => {
  let data = {
    id: devId,
  };
  let res = await uni.$u.http.post("/wechat/getDevInfo", data);

  if (res.success) {
    details.value = res.data;
    paramsList.value.forEach((item) => {
      if (item.name == "温度校正") {
        item.txt = details.value.tempAdj || 0;
      }
      if (item.name == "温度上限") {
        item.txt = details.value.tempSetTop || 50;
      }
      if (item.name == "温度下限") {
        item.txt = details.value.tempSetBot || 5;
      }
      if (item.name == "温控容差") {
        item.txt = details.value.tolr || 2;
      }
      if (item.name == "过热保护温度") {
        item.txt = details.value.overheat || 60;
      }
      if (item.name == "过热保护容差") {
        item.txt = details.value.otolr || 5;
      }
      if (item.name == "防冻温度") {
        item.txt = details.value.antifreeze || "关闭防冻";
      }
      if (item.name == "加热器保护触发时间") {
        item.txt = details.value.triggerTm || 0;
      }
      if (item.name == "加热器保护时间") {
        item.txt = details.value.protectTm || 0;
      }
      if (item.name == "湿度校准") {
        item.txt = details.value.humAdj || 0;
      }
      if (item.name == "电压保护阈值") {
        if (details.value.overVoltage == 0) {
          item.txt = "关闭";
        } else {
          item.txt = details.value.overVoltage;
        }
      }
      if (item.name == "功率保护阈值") {
        if (details.value.overPower == 0) {
          item.txt = "关闭";
        } else {
          item.txt = details.value.overPower;
        }
      }
      if (item.name == "电压校准") {
        item.txt = details.value.volAdj || 0;
      }
      if (item.name == "功率校准") {
        item.txt = details.value.powerAdj || 0;
      }
      if (item.name == "继电器反向设置") {
        if (details.value.invert == "1") {
          item.txt = "反向";
        } else {
          item.txt = "同向";
        }
      }
      if (item.name == "断电记忆") {
        if (details.value.powerRem == "2") {
          item.txt = "上电开机";
        } else if (details.value.powerRem == "1") {
          item.txt = "上电关机";
        } else {
          item.txt = "断电记忆";
        }
      }
    });
  }
};
</script>

<style scoped lang="scss">
.params-page {
  padding-top: 15rpx;
  width: 100%;
  // height: 100vh;
  background-color: #f1f1f1;
  padding-bottom: 40rpx;
}

.params-contain {
  width: 90vw;
  margin: 20rpx auto;
  display: flex;
  align-items: center;
  font-size: 30rpx;
  line-height: 70rpx;
  padding: 10rpx 0;
  background-color: #fff;
  border-radius: 10px;

  // 名称
  .params-name {
    width: 21vw;
    text-align: left;
    padding-left: 3vw;
    font-size: 28rpx;
    line-height: 28rpx;
  }

  .params-detail {
    width: 35vw;
    display: flex;
    justify-content: space-between;
    border: 1rpx solid #e1e1ee;
    padding: 0 25rpx;
    border-radius: 10rpx;
    margin: 10rpx 4vw 1rpx 10rpx;
    margin-top: 1rpx;

    ::v-deep .u-icon {
      padding-top: 6px;
    }
  }

  .params-btn {
    width: 16vw;

    ::v-deep .u-button {
      height: 70rpx;
    }
  }
}
</style>
